//
//  QHScrollSegmentView.m
//  Bilibili~
//
//  Created by lanou on 16/5/10.
//  Copyright © 2016年 裴启航. All rights reserved.
//

#import "QHScrollSegmentView.h"
#import "UIView+QHExtension.h"

@interface QHScrollSegmentView () {
    CGFloat currentWidth;
    CGFloat oldOffSetX;
}


@property (nonatomic, strong) NSArray *titleArr; // 接收标题数组
@property (nonatomic, strong) NSMutableArray *titleLabels; // 接收设置标题之后的label数组
@property (nonatomic, strong) UIScrollView *scrollView;

@property (weak, nonatomic) UIView *scrollLine;  // title下方的滑动条
@property (strong, nonatomic) NSArray *deltaRGB; // 选择和一般状态的颜色差
@property (strong, nonatomic) NSArray *selectedColorRgb; // 选择状态的字体颜色
@property (strong, nonatomic) NSArray *normalColorRgb; // 默认状态的字体颜色
@end
@implementation QHScrollSegmentView
#pragma mark - 懒加载
- (UIView *)scrollLine {        // 滚动条
    if (!_scrollLine) {
        UIView *lineView = [[UIView alloc] init];
        /** 滚动条的颜色 */
        lineView.backgroundColor = [UIColor redColor];
        [self.scrollView addSubview:lineView];
        _scrollLine = lineView;
    }
    return _scrollLine;
}
- (UIScrollView *)scrollView {  // 滚动的滑块
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.bounces = YES;
        _scrollView.pagingEnabled = NO;
        [self addSubview:_scrollView];
    }
    return _scrollView;
}
- (NSArray *)titleArr {         // 滚动的标题
    if (!_titleArr) {
        _titleArr = [NSMutableArray array];
    }
    return _titleArr;
}
- (NSMutableArray *)titleLabels {
    if (!_titleLabels) {
        self.titleLabels = [NSMutableArray array];
    }
    return _titleLabels;
}
- (NSArray *)normalColorRgb {
    if (!_normalColorRgb) {
        /**标题默认状态的颜色 */
        _normalColorRgb = [self getColorRgb:QHColor(127, 127, 127)];
    }
    return _normalColorRgb;
}
- (NSArray *)selectedColorRgb {
    if (!_selectedColorRgb) {
        /**标题选中状态的颜色 */
        _selectedColorRgb = [self getColorRgb:[UIColor redColor]];
    }
    return _selectedColorRgb;
}
- (NSArray *)deltaRGB {
    if (!_deltaRGB) {
        if (_normalColorRgb && _selectedColorRgb) {
            CGFloat deltaR = [_normalColorRgb[0] floatValue] - [_selectedColorRgb[0] floatValue];
            CGFloat deltaG = [_normalColorRgb[1] floatValue] - [_selectedColorRgb[1] floatValue];
            CGFloat deltaB = [_normalColorRgb[2] floatValue] - [_selectedColorRgb[2] floatValue];
            _deltaRGB = [NSArray arrayWithObjects:@(deltaR), @(deltaG), @(deltaB), nil];
        }
    }
    return _deltaRGB;
}
- (NSArray *)getColorRgb:(UIColor *)color {
    CGFloat numOfcomponents = CGColorGetNumberOfComponents(color.CGColor);
    NSArray *rgbComponents;
    if (numOfcomponents == 4) {
        const CGFloat *components = CGColorGetComponents(color.CGColor);
        rgbComponents = [NSArray arrayWithObjects:@(components[0]), @(components[1]), @(components[2]), nil];
    }
    return rgbComponents;
}
#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles index:(NSInteger)index {
    if (self = [super initWithFrame:frame]) {
        self.titleArr = titles;
        _currentIndex = index;
        _oldIndex = index;
        currentWidth = frame.size.width;
        oldOffSetX = 0.0;
        // 显示标题
        [self reloadTitles];
        [self setupUI];
    }
    return self;
}

- (void)reloadTitles {
    NSInteger index = 0;
    for (NSString *title in self.titleArr) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.tag = index;
        label.text = title;
        label.textColor = QHColor(127, 127, 127);
        label.font = [UIFont systemFontOfSize:14];
        label.textAlignment = NSTextAlignmentCenter;
        label.userInteractionEnabled = YES;
        // 添加点击手势
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(titleClick:)];
        [label addGestureRecognizer:tapGes];
        label.frame = CGRectMake(index * (currentWidth / self.titleArr.count), 20, currentWidth / self.titleArr.count, self.height - 2);
        [self.titleLabels addObject:label]; // 添加到数组里
        [self.scrollView addSubview:label];
        index++;
    }
}
- (void)setupUI {
    self.scrollLine.frame = CGRectMake(0, self.height - 2, ScreenWidth / self.titleArr.count, 2);
    self.scrollView.frame = CGRectMake(0, 0, currentWidth, self.height);
    // 初始进来是坐标为几开始的
    [self updateUIWithProgress:1.0 oldIndex:_oldIndex currentIndex:_currentIndex];
}
- (void)titleClick:(UITapGestureRecognizer *)sender {
    // 获取点击的UILabel
    UILabel *currentLabel = (UILabel *)sender.view;
    if (!currentLabel) {
        return;
    }
    _currentIndex = currentLabel.tag;
    // 创建通知 告诉主视图也要切换下标对应的视图
    [[NSNotificationCenter defaultCenter] postNotificationName:indexChangedNotification object:nil];
    [self updateUIWithAnimate:YES];
}

// 点击按钮后更新UI
- (void)updateUIWithAnimate:(BOOL)animate {
    if (_currentIndex == _oldIndex) {
        return;
    }
    UILabel *oldLabel = self.titleLabels[_oldIndex];
    UILabel *currentLabel = self.titleLabels[_currentIndex];
    CGFloat animatedTime = animate ? 0.25 : 0.0;
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:animatedTime animations:^{
        /** 标题一般状态的颜色 */
        oldLabel.textColor = QHColor(127, 127, 127);
        /** 标题选中状态的颜色 */
        currentLabel.textColor = QHGlobalBg;
        if (weakSelf.scrollLine) {
            weakSelf.scrollLine.x = currentLabel.x;
            oldOffSetX = weakSelf.scrollLine.x;
            weakSelf.scrollLine.width = currentLabel.width;
        }
        
    }];
    _oldIndex = _currentIndex;
}
// 滑动主界面的时候根据偏移量来更新UI
- (void)updateUIWithProgress:(CGFloat)progress oldIndex:(NSInteger)oldIndex currentIndex:(NSInteger)currentIndex {
    _oldIndex = currentIndex;
    UILabel *oldLabel = (UILabel *)self.titleLabels[oldIndex];
    UILabel *currentLabel = (UILabel *)self.titleLabels[currentIndex];
    
    CGFloat xDistance = currentLabel.x - oldLabel.x;
    CGFloat wDistance = currentLabel.width - oldLabel.width;
    
    if (self.scrollLine) {
        self.scrollLine.x = oldLabel.x + xDistance * progress;
        self.scrollLine.width = oldLabel.width + wDistance * progress;
    }
    oldLabel.textColor = [UIColor colorWithRed:[self.selectedColorRgb[0] floatValue] + [self.deltaRGB[0] floatValue] * progress green:[self.selectedColorRgb[1] floatValue] + [self.deltaRGB[1] floatValue] * progress blue:[self.selectedColorRgb[2] floatValue] + [self.deltaRGB[2] floatValue] * progress alpha:1.0];
    currentLabel.textColor = [UIColor colorWithRed:[self.normalColorRgb[0] floatValue] - [self.deltaRGB[0] floatValue] * progress green:[self.normalColorRgb[1] floatValue] - [self.deltaRGB[1] floatValue] * progress blue:[self.normalColorRgb[2] floatValue] - [self.deltaRGB[2] floatValue] * progress alpha:1.0];
}


@end
